//
//  YMCommentEmergencyView.m
//  YMCommentDemo
//
//  Created by lantaiyuan on 2017/2/27.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//
#define Normal_color HEX(@"#333333")
#define Hightlight_color HEX(@"#FA9338")

#import "YMCommentEmergencyView.h"

@interface YMCommentEmergencyView ()
@property (nonatomic, strong) UIView * topView;
@property (nonatomic, strong) UIView * tagsView;
@property (nonatomic, strong) UIView * photoView;
@property (nonatomic, strong) UIView * contentView;
@property (nonatomic, strong) UIButton * sendBtn;
@property (nonatomic, strong) NSMutableArray * selTags;
@end

@implementation YMCommentEmergencyView

#pragma mark - Lazy


- (NSMutableArray *)selTags{
    if (!_selTags) {
        _selTags = [NSMutableArray array];
    }
    return _selTags;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
        _topView.backgroundColor = [UIColor whiteColor];
        UIImageView * backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, (44 - 18)/2, 9, 18)];
        backImageView.image = [UIImage imageNamed:@"icon_back_r"];
        [_topView addSubview:backImageView];
        
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [btn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchDown];
        [_topView addSubview:btn];
        
        UILabel * titleLabel = [self labelWithText:@"突发事件" textColor:HEX(@"#333333") font:[UIFont systemFontOfSize:15] height:_topView.frame.size.height orginX:0];
        [_topView addSubview:titleLabel];
        titleLabel.center = _topView.center;
    }
    return _topView;
}

- (UIView *)tagsView{
    if (!_tagsView) {
        _tagsView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.topView.frame) + 1, self.frame.size.width, 90)];
        _tagsView.backgroundColor = [UIColor whiteColor];
        CGFloat W = (self.frame.size.width - 60) / 3.0;
        CGFloat H = 30;
        CGFloat space = 15;
        
        NSArray * titles = @[@"医疗紧急情况",@"安全事故",@"火警",@"其他"];
        
        for (NSInteger i = 0; i < titles.count; i ++) {
            NSInteger row = i / 3;
            NSInteger col = i % 3;
            UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(col * (W + space) + space , row * (H + 10) + 10, W, H)];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            [btn setTitleColor:Normal_color forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            btn.layer.cornerRadius = 15.0;
            btn.layer.borderWidth = 1.0;
            btn.layer.borderColor = HEX(@"#bbbbbb").CGColor;
            [btn addTarget:self action:@selector(selTag:) forControlEvents:UIControlEventTouchDown];
            [_tagsView addSubview:btn];
        }
    }
    return _tagsView;
}

- (UIView *)photoView{
    if (!_photoView) {
        _photoView = [[UIView alloc] init];
        _photoView.frame = CGRectMake(0, self.frame.size.height - 75 - 44, self.frame.size.width, 75);
        _photoView.backgroundColor = [UIColor cyanColor];
    }
    return _photoView;
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        
        CGFloat y = CGRectGetMaxY(self.tagsView.frame);
        CGFloat H = CGRectGetMinY(self.photoView.frame) - y;
        
        _contentView.frame = CGRectMake(0, y, self.frame.size.width, H);
        _contentView.backgroundColor = [UIColor whiteColor];
        
        YYTextView * tf = [[YYTextView alloc] initWithFrame:CGRectMake(15, 0, _contentView.bounds.size.width - 30, _contentView.bounds.size.height)];
        tf.backgroundColor = [UIColor whiteColor];
        tf.placeholderText = @"更多细节";
        tf.placeholderFont = [UIFont systemFontOfSize:12];
        tf.font = [UIFont systemFontOfSize:12];
        tf.textColor = HEX(@"#999999");
        [_contentView addSubview:tf];
    }
    return _contentView;
}

- (UIButton *)sendBtn{
    if (!_sendBtn) {
        _sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 44, self.frame.size.width, 44)];
        [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [_sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _sendBtn.backgroundColor = HEX(@"#2ab650");
    }
    return _sendBtn;
}

- (UILabel *)labelWithText:(NSString *)text
                 textColor:(UIColor *)textColor
                      font:(UIFont *)font
                    height:(CGFloat)height
                    orginX:(CGFloat)orginX
{
    UILabel * label = [[UILabel alloc] init];
    label.text = text;
    label.textColor = textColor;
    label.font = font;
    CGSize size = [self boundingRectWithString:label.text font:label.font height:height];
    label.frame = CGRectMake(orginX, 0, size.width, height);
    return label;
}



#pragma mark - init

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = HEX(@"#f5f5f5");
        [self addSubview:self.topView];
        [self addSubview:self.tagsView];
        [self addSubview:self.sendBtn];
        [self addSubview:self.photoView];
        [self addSubview:self.contentView];
    }
    return self;
}

#pragma mark - Private

- (void)backBtnAction{
    if (_backBlock) {
        _backBlock();
    }
}

- (void)sendBtnAction:(UIButton *)sender{
    YMCommentSendType sendType = YMCommentSendTypeEmergency;
    NSDictionary * userInfo = @{@"sendtype":@(sendType)};
    [[NSNotificationCenter defaultCenter] postNotificationName:Comment_send_action object:nil userInfo:userInfo];
}

- (CGSize)boundingRectWithString:(NSString *)string font:(UIFont *)font height:(CGFloat)height{
    NSMutableDictionary * attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(MAXFLOAT, height);
    CGSize size = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
    return size;
}

- (void)selTag:(UIButton *)sender{
    
    
    __block NSMutableArray * temps = [NSMutableArray arrayWithArray:self.selTags];
    
    [self.selTags enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isEqualToString:sender.titleLabel.text]) {
            [temps removeObject:obj];
        }
    }];
    
    [self.selTags removeAllObjects];
    [self.selTags addObjectsFromArray:temps];
    
    if ([sender.titleLabel.textColor isEqual:Hightlight_color]) {
        [sender setTitleColor:Normal_color forState:UIControlStateNormal];
        sender.layer.borderColor = HEX(@"#bbbbbb").CGColor;
    }else{
        [sender setTitleColor:Hightlight_color forState:UIControlStateNormal];
        sender.layer.borderColor = Hightlight_color.CGColor;
        [self.selTags addObject:sender.titleLabel.text];
    }
    
}

@end
